//
//  LWCityController.m
//  豆瓣
//
//  Created by 刘伟 on 16/7/6.
//  Copyright © 2016年 刘伟. All rights reserved.
//
#import "LWHeaderView.h"
#import "LWCityController.h"
#import "DXPopover.h"
#import "ActivityTypeView.h"
#import "LWSearchCityController.h"
#import "LWHttpTool.h"
@interface LWCityController ()<UITableViewDelegate,UITableViewDataSource,LWHeaderViewDelegate>
@property (nonatomic, strong) DXPopover *popover;
@property(nonatomic,weak)UIButton * btn;
@property(nonatomic,strong)NSMutableArray * dataArr;
@property(nonatomic,strong) UITableView * tableView;
@end

@implementation LWCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArr = [NSMutableArray arrayWithCapacity:1];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(typeclick:) name:@"selete" object:nil];
    
    //设置titleview
   [self setNav];
    //设置城市选择器
    [self setcity];
}
-(void)typeclick:(NSNotification*)notice{
    NSString*str = notice.userInfo[@"title"];
    　[self.popover dismiss];
    self.popover = nil;
    [self.btn setTitle:str forState:UIControlStateNormal];
 

}
//设置titleview
-(void)setNav{
    UIButton *btn = [[UIButton alloc]init];
    btn.x =0;
    btn.y = 0;
    btn.width = 70;
    btn.height = 40;
    self.btn = btn;
      NSDictionary *dict = [[NSDictionary alloc] initWithObjects:@[@"热门",@"音乐",@"戏剧",@"展览",@"讲座",@"聚会",@"运动",@"旅行",@"公益",@"电影"] forKeys:@[@"all",@"music",@"drama",@"exhibition",@"salon",@"party", @"sports", @"travel", @"commonweal",@"film"] ];
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"type"]) {
         [btn setTitle:@"热门" forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setObject:@"all" forKey:@"type"];
    }else{
        NSString * str = [dict valueForKey:[[NSUserDefaults standardUserDefaults] objectForKey:@"type"]];
        [btn setTitle:str forState:UIControlStateNormal];
   }
    
  
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 50, 0, 0)];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
   
     [btn setImage:[UIImage imageNamed:@"LuckyMoney_ChangeArrow"] forState:UIControlStateNormal];
    [btn setTitleColor:TheThemeColor forState:UIControlStateNormal];
    self.navigationItem.titleView = btn;
    
    [btn addTarget:self action:@selector(didtitle:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)didtitle:(UIButton*)sender{
    if (self.popover) {
        [self.popover dismiss];
        self.popover = nil;
        return;
    }
    DXPopover*pop = [DXPopover new];
    pop.maskType = DXPopoverMaskTypeBlack;
    ActivityTypeView * active = [[ActivityTypeView alloc] initWithFrame:CGRectMake(0, 0, 130, 200)];
    self.popover = pop;
    UIView *titleView = self.navigationItem.titleView;
    CGPoint startPoint =
    CGPointMake(CGRectGetMidX(titleView.frame), CGRectGetMaxY(titleView.frame) + 20);
    [pop showAtPoint:startPoint popoverPostion: DXPopoverPositionDown withContentView:active inView:self.view];
    pop.didDismissHandler = ^{
        self.btn.transform = CGAffineTransformScale(self.btn.transform, 0.8, 0.8);
        [UIView animateWithDuration:0.5 animations:^{
            self.btn.transform = CGAffineTransformIdentity;
        }];
    };
  
}
-(void)setcity{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = NO;
    self.tableView.backgroundColor = KColor(233, 233, 233);
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;

    LWHeaderView *v = [[LWHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    v.delegate = self;
    self.tableView.contentInset = UIEdgeInsetsMake(30, 0, 0, 0);
    self.tableView.tableHeaderView = v;
  

}

-(void)seletedCity:(UILabel *)cityLb{
    LWSearchCityController *city = [LWSearchCityController new]
    ;
    [self.navigationController pushViewController:city animated:YES];
    
   

}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
